//
//  HZProductTextImageDetailCell.m
//  app
//
//  Created by 刘华舟 on 15/6/3.
//  Copyright (c) 2015年 hdaren. All rights reserved.
//

#import "HZProductTextImageDetailCell.h"
#import "HZProductTextImageDetailModel.h"

#import "HZTableView.h"
#import "HZProductTextImageDetailModel.h"
#import "UIButton+URL.h"
#import "UIImageView+Conveniently.h"

static const NSUInteger kTitleLabelHeight = kProductTextImageDetailTitleLabelHeight;
static const NSUInteger kTextMaxHeight = kProductTextImageDetailTextMaxHeight;

@interface HZProductTextImageDetailCell()


@property (strong, nonatomic) UILabel *titleLabel;
@property (strong, nonatomic) UILabel *textsLabel;
@property (strong, nonatomic) UIImageView *picView;


@end

@implementation HZProductTextImageDetailCell

+ (instancetype)cellWithTableView:(HZTableView *)tableView{
    NSString *cellReuseIdentifier = @"HZProductTextImageDetailCell";
    
    HZProductTextImageDetailCell * cell= (HZProductTextImageDetailCell *)[tableView dequeueReusableCellWithIdentifier:cellReuseIdentifier];
    if (cell == nil) {
        cell = [[HZProductTextImageDetailCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellReuseIdentifier];
    }
    return cell;
}


- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.frame = CGRectOf([self.ubo fx:UB_LEFT fy:UB_TOP], [UBO_SCREEN fw:UB_WIDTH uh:0]);
        
        self.backgroundColor = HZ_WHITE_COLOR;
        
        //标题
        
        self.titleLabel = [[UILabel alloc] initWithFrame:CGRectOf([self.ubo fx:UB_LEFT ux:kSizeMallViewMargin fy:UB_TOP],
                                                                 [self.ubo fw:UB_WIDTH uw:-2*kSizeMallViewMargin uh:0])];
        
        
        self.titleLabel.textAlignment = NSTextAlignmentLeft;
        self.titleLabel.textColor = HZ_MALL_TEXT_L_LIGHTGRAY_COLOR;
        self.titleLabel.font = JKAppGlobleFontContentText;
        [self addSubview:self.titleLabel];
        
        //内容
        
        self.textsLabel = [[UILabel alloc] initWithFrame:CGRectOf([self.titleLabel.ufo fx:UB_LEFT fy:UB_BOTTOM],
                                                                  [self.titleLabel.ubo fw:UB_WIDTH uh:0])];
        
        
        self.textsLabel.textAlignment = NSTextAlignmentLeft;
        self.textsLabel.textColor = HZ_MALL_TEXT_L_LIGHTGRAY_COLOR;
        self.textsLabel.font = JKAppGlobleFontSmallTagText;
        self.textsLabel.numberOfLines = 0;
        [self addSubview:self.textsLabel];
        
        //图片
        //内容
        
        self.picView = [[UIImageView alloc] initWithFrame:CGRectOf([self.textsLabel.ufo fx:UB_LEFT fy:UB_BOTTOM],
                                                                 [self.textsLabel.ubo fw:UB_WIDTH uh:0])];
        UITapGestureRecognizer *tapGestr = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(picBtnOnClick)];
        self.picView.userInteractionEnabled = YES;
        [self.picView addGestureRecognizer:tapGestr];
        self.picView.contentMode = UIViewContentModeScaleAspectFit;

        [self addSubview:self.picView];
    }
    return self;
}

#pragma mark 点击图片
- (void)picBtnOnClick{
    if (self.picsTargetCaller) {
        [self.picsTargetCaller call:self.model];
    }
}

- (void)setModel:(HZProductTextImageDetailModel *)model{
    _model = model;
    
    if (model.title.length) {
        self.titleLabel.frame = CGRectOf([self.ubo fx:UB_LEFT ux:kSizeMallViewMargin fy:UB_TOP],
                                         [self.ubo fw:UB_WIDTH uw:-2*kSizeMallViewMargin uh:kTitleLabelHeight]);
        self.titleLabel.text = model.title;
    }else{
        self.titleLabel.frame = CGRectOf([self.ubo fx:UB_LEFT ux:kSizeMallViewMargin fy:UB_TOP],
                                         [self.ubo fw:UB_WIDTH uw:-2*kSizeMallViewMargin uh:0]);
        self.titleLabel.text = @"";
    }
    
    
    //评论
    if (model.text.length) {
        CGSize contentTextSize = [model.text boundingRectWithSize:[UBO_SCREEN fw:UB_WIDTH uw:-(kSizeMallViewMargin*2) uh:kTextMaxHeight] options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:JKAppGlobleFontSmallTagText} context:nil].size;
        
        self.textsLabel.frame = CGRectOf([self.titleLabel.ufo fx:UB_LEFT fy:UB_BOTTOM],
                                            [self.titleLabel.ubo fw:UB_WIDTH oh:contentTextSize.height]);
        self.textsLabel.text = model.text;
    }else{
        self.textsLabel.frame = CGRectOf([self.titleLabel.ufo fx:UB_LEFT fy:UB_BOTTOM],
                                        [self.titleLabel.ubo fw:UB_WIDTH oh:0]);
        self.textsLabel.text = @"";
    }
    //图片
    if (model.pic.length) {
        self.picView.frame = CGRectOf([self.textsLabel.ufo fx:UB_LEFT fy:UB_BOTTOM oy:6],
                                     [self.ubo fw:UB_WIDTH uw:-(kSizeMallViewMargin*2) oh:([UBO_SCREEN fw:UB_WIDTH uw:-(kSizeMallViewMargin*2)]*[model.height floatValue]/[model.width floatValue])]);
        
        [self.picView setFadeOutImageURL:model.pic placehoderImage:nil duration:0.2];
        self.picView.hidden = NO;
    }else{
        self.picView.frame = CGRectOf([self.textLabel.ufo fx:UB_LEFT fy:UB_BOTTOM],
                                     [self.ubo fw:UB_WIDTH uw:-2*kSizeMallViewMargin oh:0]);
        self.picView.hidden = YES;
    }
    
}

@end
